package com.heima.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.comment.mapper.ApCommentShowMapper;
import com.heima.comment.pojo.ApCommentShow;
import com.heima.comment.service.ApCommentShowService;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-04-10
 */
@Service
public class ApCommentShowServiceImpl extends ServiceImpl<ApCommentShowMapper, ApCommentShow> implements ApCommentShowService {


    /**
     * 查看当前用户是否对指定评论点赞
     *
     * @param commentId
     * @return 是返回true, 不是返回false
     */
    @Override
    public boolean isLike(Integer commentId) {

        Integer userId = UserThreadLocalUtils.getUserIdAndCheckLogin();
        // 如果userId或commentId有一者为null 则返回false
        if (userId <= 0 || commentId == null) {
            return false;
        }
        LambdaQueryWrapper<ApCommentShow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApCommentShow::getCommentId, commentId);
        queryWrapper.eq(ApCommentShow::getCommentUserId, userId);
        ApCommentShow commentShowDB = this.getOne(queryWrapper);

        // 如果commentShowDB 不为null 且 commentShowDB.getIsGiveUp() 不为false 返回true
        if (commentShowDB == null) {
            return false;
        }
        return commentShowDB.getIsGiveUp() != null && commentShowDB.getIsGiveUp().equals(true);
    }
}